do DialogDemoWindow.{show-at-center};

do DialogDemoWindow.{on-close}.{connect {os::exit 0}};

do
    \ @text := sync { Reactive '' },
    let @line := { merge [@open, @open-multiple, @open-directory, @save] },
    (LogBox bind @text)
        . { with @line
            .{ connect &(line) =>
                @text.{update &(text) => {"??"..\n (text,line)}} }
            };


const @open: Source[String] :=
    ButtonOpen.{activated}
        . { concat-map &() =>
            { ui::dialog-open {
                parent: { Some DialogDemoWindow },
                title:  'Open',
                cwd:    os::Cwd,
                filter: 'Text files (*.txt);;Markdown files (*.md)'
            } } }
        . { map &(path?) =>
            switch path?:
            case Some path:
                { "open: ?" path.{String} },
            case None:
                'open: (cancelled)',
            end
        };

const @open-multiple: Source[String] :=
    ButtonOpenMultiple.{activated}
        . { concat-map &() =>
            { ui::dialog-open-multiple {
                parent: { Some DialogDemoWindow },
                title:  'Open Multiple',
                cwd:    os::Cwd,
                filter: 'Text files (*.txt)'
            } } }
        . { map &(path-list) =>
            if (path-list.{length} > 0):
                { "open multiple: ?"
                    path-list
                        . { map &(path) => path.{String} }
                        . { map "'?'" }
                        . { join ' ' } },
            else:
                'open multiple: (cancelled)'
        };

const @open-directory: Source[String] :=
    ButtonOpenDirectory.{activated}
        . { concat-map &() =>
            { ui::dialog-open-directory {
                parent: { Some DialogDemoWindow },
                title:  'Open Directory',
                cwd:    os::Cwd
            } } }
        . { map &(path?) =>
            switch path?:
            case Some path:
                { "open directory: ?" path.{String} },
            case None:
                'open directory: (cancelled)',
            end
        };

const @save: Source[String] :=
    ButtonSave.{activated}
        . { concat-map &() =>
            { ui::dialog-save {
                parent: { Some DialogDemoWindow },
                title:  'Save',
                cwd:    os::Cwd,
                filter: 'Text files (*.txt)'
            } } }
        . { map &(path?) =>
            switch path?:
            case Some path:
                { "save: ?" path.{String} },
            case None:
                'save: (cancelled)',
            end
        };
